#pragma once

#include "../libs/httplib.h"
#include "../libs/easylog.h"
#include "../util.hpp"
#include "../controllers/usercontroller.hpp"

namespace pharmacy_server
{
    struct UserBusiness
    {
        static void login_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("loginUser body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto username = root["username"].asString();
                auto password = root["password"].asString();
                auto role = root["role"].asString();

                Result result = UserController::instance().login_user(username, password, role);
                rsp.set_content(result.serialize(), "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("loginUser business error: {}", e.what());
            }
        }

        static void register_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("registerUser body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto username = root["username"].asString();
                auto password = root["password"].asString();
                auto role = root["role"].asString();

                Result result = UserController::instance().register_user(username, password, role);
                rsp.set_content(result.serialize(), "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("registerUser business error: {}", e.what());
            }
        }

        static void delete_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("deleteUser body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto username = root["username"].asString();

                rsp.set_content(UserController::instance().delete_user(username).serialize(),
                    "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("deleteUser business error: {}", e.what());
            }
        }

        static void update_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("updateUser body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto username = root["username"].asString();
                auto new_username = root["new_username"].asString();
                auto new_password = root["new_password"].asString();
                auto new_role = root["new_role"].asString();

                Result result = UserController::instance().update_user(username, new_username, new_password, new_role);
                rsp.set_content(result.serialize(), "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("updateUser business error: {}", e.what());
            }
        }

        static void get_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("getUser body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto username = root["username"].asString();

                rsp.set_content(UserController::instance().get_user(username).serialize(),
                    "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("getUser business error: {}", e.what());
            }
        }

        static void current_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("currentUser body: {}", req.body);

                rsp.set_content(UserController::instance().current_user().serialize(),
                    "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("currentUser business error: {}", e.what());
            }
        }

        static void logout_user(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("logoutUser");

                rsp.set_content(UserController::instance().logout_user().serialize(),
                    "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("logoutUser business error: {}", e.what());
            }
        }
    };
}